<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta charset="utf-8"/>
    <title>后台管理系统 -- 用户登录</title>
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/assets/css/login.css" media="all">
</head>
<body>
<div class="login-wrapper">

    <div class="login-header">
        <br>
    </div>

    <div class="login-body">
        <div class="layui-card">
            <div class="layui-card-header">
                <i class="layui-icon layui-icon-engine"></i>&nbsp;&nbsp;用户登录
            </div>
            <div class="layui-card-body layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-username"></i></label>
                    <div class="layui-input-block">
                        <input name="loginName" maxlength="18" type="text" lay-verify="required" placeholder="请输入登录账号"
                               value="18154579950" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><i class="layui-icon layui-icon-password"></i></label>
                    <div class="layui-input-block">
                        <input name="password" maxlength="18" type="password" lay-verify="required" placeholder="请输入登录密码"
                               value="123456" class="layui-input">
                    </div>
                </div>
               
                <div class="layui-form-item">
                	<label class="layui-form-label"><i class="layui-icon layui-icon-vercode"></i></label>
                	<div class="layui-input-block">
	                    <input name="code" maxlength="6" type="text" style="width:176px;float:left;"
	                        lay-verify="required" placeholder="请输入验证码"
	                               class="layui-input" />
	                     <img id="form-login-vimg"
	                     	style="display:block;float:left;height:38px;">
                    </div>
                </div>
                <div class="layui-form-item">
                    <button lay-filter="login-submit" class="login-submit layui-btn layui-btn-fluid" lay-submit>登 录</button>
                </div>
                <div class="layui-form-item login-other">

                </div>
            </div>
        </div>
    </div>
    
    <div class="login-footer">
        <p>© 2019-2020 CopyRight Taller </p>
    </div>
</div>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="/assets/libs/layui/layui.js"></script>
<script src="/assets/libs/pandyle.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.all.min.js"></script>
<script src="/module/global.js"></script>

<script>
    var key = "";
	initialize({
		init: function(){
            initVImg();
		    // 判断token是否存在
            let token = soda.session.getToken();
            if((typeof token) != "undefined"){
                var _param = {
                    url: '/v1/common/token/1',
                    err: function(){
                        soda.session.clean();
                    },
                    tips: 0,
                    callback: function(data){
                        soda.session.putToken(data.token);
                        soda.session.putUser(data.user);
                        window.location.href = '/admin.html'
                    }
                }
                soda.http.get(_param);
            }
            $('body').bind('keyup', function(event) {
                if (event.keyCode == "13") {
                    $('.login-submit').click();
                }
            });
            $('#form-login-vimg').click(function () {
                initVImg();
            })
			form.on('submit(login-submit)', function (obj) {
				obj.field.code = $("input[name='code']").val();
				var _param = {
                    url: '/v1/common/login/1',
                    data: JSON.stringify(obj.field),
                    token: key,
                    tips: 0,
                    err: function(){
                        $("input[name='code']").val('');
                        $('#form-login-vimg').click();
                    },
                    callback: function(data){
                        soda.session.putToken(data.token);
                        soda.session.putUser(data.user);
                        window.location.href = '/admin.html'
                    }
                }
				soda.http.post(_param);
			});

		}
	})
    function initVImg() {
        let img = action + '/v1/common/vimg?key=' + soda.random.str(16);
        $.ajax({
            url: action + '/v1/common/img?url=' + img,
            method: 'get',
            dataType: 'json',
            success: function (d) {
                var img = 'data:image/gif;base64,' + d.result.img;
                $('#form-login-vimg').attr('src', img);
                key = d.result.token;
            }
        })

    }
</script>
</body>
</html>